package com.rtx.wms.customer.k3.base.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.rtx.wms.customer.k3.base.service.AbsBasicSchedulerAPI;
import com.rtx.wms.std.sku.bean.po.Sku;
import com.rtx.wms.std.sku.service.SkuService;

@Component
public class MaterialHandle extends AbsBasicSchedulerAPI{
	
	 ThreadPoolExecutor pool = new ThreadPoolExecutor(10, 100, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(100));
	
	Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	SkuService skuService;
	
	@Override
	protected String getConfigCode() {
		return "wms-base-material";
	}

	@Override
	protected String getK3FormId() {
		return "BD_MATERIAL";
	}

	@Override
	protected String getK3Fields() {
		return "FNUMBER,FNAME,FMATERIALGROUP,FMATERIALGROUP,F_FSCA_DECIMAL,FSpecification";
	}

	@Override
	protected String getWmsAPIMappingCode() {
		return "k3_wms_sku_create_std";
	}

	public String queryMaterial(String requestStr) throws Exception {
		JSONObject request = JSONObject.parseObject(requestStr);
		List<String> skucodeList =Arrays.asList(request.getString("sku"));
		List<String> k3ResponseList = new ArrayList<String>();
		for(String skucode:skucodeList) {
			String k3reponse = queryByCondition(skucode);
			List<String> requestList = JSON.parseArray(k3reponse, String.class);
			k3ResponseList.addAll(requestList);			
		}
		//异步下发到wms
		pool.execute(()->{
            callWmsAPI(k3ResponseList);
        });
		
		return JSON.toJSONString(k3ResponseList);
	}
	
	
	
	public String queryByCondition(String skucode) throws Exception {
		JSONArray filter = new JSONArray();
		JSONObject filter1 = new JSONObject();
		filter1.put("Left", "("); 
		filter1.put("FieldName", "FNUMBER"); 
		filter1.put("Compare", "="); 
		filter1.put("Value", skucode); 
		filter1.put("Right", ")"); 
		filter1.put("Logic", ""); 
		filter.add(filter1);
		
		K3CloudApi api = k3Api.getK3API();
		
		Map<String,Object> request = new JSONObject();
		request.put("FormId", getK3FormId());
		request.put("FieldKeys", getK3Fields());
		request.put("FilterString", filter);
		request.put("OrderString", "");
		request.put("TopRowCount", 100);
		request.put("StartRow", 0);
		request.put("Limit", 100);
		request.put("SubSystemId", "");
		
		String requestStr = JSON.toJSONString(request);
		
		String result = api.billQuery(requestStr);
		return result;
	}

	@Override
	protected void callWmsAPI(List<String> requestList) {
		for(String request:requestList) {
			try {
				Sku sku = mappingToSku(request);
				
				skuService.addSku("enterprise", sku);
			} catch (Exception e) {
				log.error("",e);
			}
		}
	
	}
	
	public Sku mappingToSku(String request) {
		JSONObject material = JSON.parseObject(request);
		Sku sku =new Sku();
		sku.setSku(material.getString("FNUMBER"));
		sku.setStorerkey("CATECH");
//		sku.setSkugroup(material.getString("FMATERIALGROUP"));
//		sku.setDatecodedays("0");
		sku.setDescr(material.getString("FNAME"));
//		sku.setRtxclass(material.getString("FSpecification"));
		sku.setPackkey("STD");
//		sku.setRtxfactory("");
//		sku.setRead_rule_id("22");
//		sku.setRead_rule_name("CATECH");
		sku.setStrategykey("CATECH");
		sku.setPutawaystrategykey("CATECH");
		sku.setLottablevalidationkey("1-4-5");
//		sku.setDatecodedays("999");
//		sku.setPickcode("NSPFIFO");
//		sku.setRotateby("Lottable04");
		sku.setLottablevalidationkey("1-4-5");
		return sku;
	}
}
